using System;
using System.Diagnostics;
using System.Globalization;
using WPFTemplateLib.WpfConverters.Core;
/*
* 源码已托管：https://gitee.com/dlgcy/WPFTemplateLib
* 版本：2025年10月30日
*/
namespace WPFTemplateLib.WpfConverters
{
	/// <summary>
	/// [转换器] 参考值转实际值（版本2：多值绑定版）
	/// <para/> 绑定顺序：原始绑定值、参考值（数据）最小值、参考值（数据）最大值、实际值（UI）最小值、实际值（UI）最大值。
	/// <example>
	/// <![CDATA[
	///	<TextBlock.Text>
	///		<MultiBinding Converter="{lib:ReferenceValueToRealValueConverterV2}">
	///			<Binding Path="BindValue"/>
	///			<Binding Path="RefValueMin"/>
	///			<Binding Path="RefValueMax"/>
	///			<Binding Path="RealValueMin"/>
	///			<Binding Path="RealValueMax"/>
	///		</MultiBinding>
	///	</TextBlock.Text>
	/// ]]>
	/// </example>
	/// </summary>
	public class ReferenceValueToRealValueConverterV2 : MultiValueConverterBase
	{
		#region 属性

		/// <summary>
		/// 是否调转方向
		/// </summary>
		public bool IsReversed { get; set; }

		/// <summary>
		/// 参考值大于最大值时是否将实际值置为另一端的数值
		/// </summary>
		public bool IsGoToAnotherEndWhenRefBiggerThenMax { get; set; }

		/// <summary>
		/// 参考值小于最小值时是否将实际值置为另一端的数值
		/// </summary>
		public bool IsGoToAnotherEndWhenRefSmallerThenMin { get; set; }

		#endregion

		public override object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
		{
			double realValue = 0;
			try
			{
				if(values == null || values.Length < 5)
				{
					throw new ArgumentException($"多值绑定数量必须为 5.");
				}

				string originValueStr = values[0] + "";
				string refValueMinStr = values[1] + "";
				string refValueMaxStr = values[2] + "";
				string realValueMinStr = values[3] + "";
				string realValueMaxStr = values[4] + "";

				if(!double.TryParse(originValueStr, out double originValue))
				{
					throw new ArgumentException($"原始绑定值[{originValueStr}] 无法转为 double");
				}

				if(!double.TryParse(refValueMinStr, out double refValueMin))
				{
					throw new ArgumentException($"参考值（数据）最小值[{refValueMinStr}] 无法转为 double");
				}

				if(!double.TryParse(refValueMaxStr, out double refValueMax))
				{
					throw new ArgumentException($"参考值（数据）最大值[{refValueMaxStr}] 无法转为 double");
				}

				if(!double.TryParse(realValueMinStr, out double realValueMin))
				{
					throw new ArgumentException($"实际值（UI）最小值[{realValueMinStr}] 无法转为 double");
				}

				if(!double.TryParse(realValueMaxStr, out double realValueMax))
				{
					throw new ArgumentException($"实际值（UI）最大值[{realValueMaxStr}] 无法转为 double");
				}

				if(originValue < refValueMin)
				{
					originValue = refValueMin;
					if(IsGoToAnotherEndWhenRefSmallerThenMin)
					{
						originValue = refValueMax;
					}
				}

				if(originValue > refValueMax)
				{
					originValue = refValueMax;
					if(IsGoToAnotherEndWhenRefBiggerThenMax)
					{
						originValue = refValueMin;
					}
				}

				double ratio = (originValue - refValueMin) / (refValueMax - refValueMin);
				double gap = ratio * (realValueMax - realValueMin);
				if(IsReversed)
				{
					realValue = realValueMax - gap;
				}
				else
				{
					realValue = gap + realValueMin;
				}
			}
			catch(Exception ex)
			{
				Console.WriteLine(ex);
				Debug.WriteLine($"[{nameof(ReferenceValueToRealValueConverter)}] Exception：{ex}");
			}

			if(targetType == typeof(string))
			{
				return realValue.ToString();
			}

			return realValue;
		}
	}
}
